package com.hdu.hot100.list;

public class lc160 {


    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

            ListNode curA = headA;
            ListNode curB = headB;

            int lenA = 0;
            int lenB = 0;

            while (curA != null) {
                lenA++;
                curA = curA.next;
            }

            while (curB != null) {
                lenB++;
                curB = curB.next;
            }

            curA = headA;
            curB = headB;

            int deltaLen = Math.abs(lenA - lenB);
            if (lenA > lenB)
                while (deltaLen-- > 0)
                    curA = curA.next;
            else
                while (deltaLen-- > 0)
                    curB = curB.next;


            while (curA != null)
            {
                if (curA == curB)
                    return curA;
                curA = curA.next;
                curB = curB.next;
            }

            return null;
        }
    }
}
